package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.mapper.AppointmentMapper;

import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.AppointmentQuery;
import cn.wolfcode.car.business.service.IAppointmentService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.BatchUpdateException;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class AppointmentServiceImpl implements IAppointmentService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private StatementMapper statementMapper;


    @Override
    public TablePageInfo<Appointment> query(AppointmentQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<Appointment>(appointmentMapper.selectForList(qo));
    }


    @Override
    public void save(Appointment appointment) {
        appointment.setCreateTime(new Date());
        appointmentMapper.insert(appointment);
    }

    @Override
    public Appointment get(Long id) {
        return appointmentMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(Appointment appointment) {
        Appointment appointment1 = this.get(appointment.getId());
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment1.getStatus())) {

            throw  new BusinessException("非法操作");
        }
        //哪些时间不需要修改，状态不需要修改
        //更新数据
        appointmentMapper.updateByPrimaryKey(appointment);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            //把数据查询出来，做状态判断(预约中)
            Appointment appointment = this.get(dictId);
            if (!appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
                throw  new BusinessException("非预约状态中的数据不可删除");
            }
            appointmentMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<Appointment> list() {
        return appointmentMapper.selectAll();
    }

   @Override
    public boolean checkNameExsit(String name) {
        return appointmentMapper.selectByName(name) != null;
    }

    @Override
    public void arralShop(Long id) {
        //获取到店状态的信息
        Appointment appointment = this.get(id);
        //判断其状态是否为预约中，非预约中的状态抛出异常
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("状态异常");
        }
        //修改其状态
        appointmentMapper.changeArralStatus(id,Appointment.STATUS_ARRIVAL);

    }

    @Override
    public void cancel(Long id) {
        //根据id查询数据
        Appointment appointment = this.get(id);
        //判断状态是否为预约中，不是预约中则抛出异常
        if (!Appointment.STATUS_APPOINTMENT.equals(appointment.getStatus())) {
            throw new BusinessException("非预约状态不可取消");
        }
        //修改状态,将状态改为用户取消状态
        appointmentMapper.changeCancelStatus(id,Appointment.STATUS_CANCEL);
    }

    @Override
    public Long generateStatement(Long id) {//此id为appointment_id
       //获取结算单中的appointment_id
        Statement statement = statementMapper.queryAppointmentId(id);
        //获取预约单信息
        Appointment appointment = appointmentMapper.selectByPrimaryKey(id);
        if (statement==null) {
            //若结算单没有此id，证明还没有生成结算单
            //创建一个结算单
            statement=new Statement();
            statement.setCreateTime(new Date());
            //记得将statementId设置进去
            statement.setAppointmentId(id);
            statement.setActualArrivalTime(appointment.getActualArrivalTime());
            statement.setCarSeries(appointment.getCarSeries());
            statement.setCustomerName(appointment.getCustomerName());
            statement.setCustomerPhone(appointment.getCustomerPhone());
            statement.setInfo(appointment.getInfo());
            statement.setLicensePlate(appointment.getLicensePlate());
            statement.setServiceType(appointment.getServiceType());
            statementMapper.insert(statement);
        }
        //获取预约单中的信息设置进去结算单，并且将结算单中的appointment_id设置进去,将结算单的状态改为已结算状态
        appointmentMapper.changeStatus(id,Appointment.STATUS_SETTLE);
        //返回statementId
        System.out.println(statement.getId());
        return statement.getId();
    }
}
